<?php
include './include/common.inc.php';
include '../admin/include/search.class.php';
$search = new search_conf();
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/search.php');
switch ($action)
{
	case 'add':
		if(check_perms('sc002')===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		if(isset($_POST['scname']))
		{
			if(empty($scname))
			{
				$response->text_alert('error','scname','配置名称不能为空');
			}
			if(empty($scsql))
			{
				$response->text_alert('error','scsql','请设置源数据获取sql');
			}
			if(empty($other['up_time_column']))
			{
				$response->text_alert('error','other_up_time_column','请设置修改时间字段名');
			}
			if(!is_numeric($other['search_step']) || intval($other['search_step']) < 0)
			{
				$response->text_alert('error','other_search_step','请设置每次搜索步长，并确定设置的值为正整数');
			}
			if(empty($other['pk_name']))
			{
				$response->text_alert('error','other_pk_name','请设置主键字段名称');
			}
			if(empty($scconf))
			{
				$response->text_alert('error','scconf','请设置地址跳转配置');
			}
			$response->real_execute();
			$data = $_POST;
			$data['otherinfo'] = $other;
			$res = $search->add($data);
			if($res === false)
			{
				$response->alert('添加失败！');
			}
			else 
			{
				$response->alert('添加成功');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			include template('admin', 'search_conf_add');
		}
		break;
	case 'edit':
		if(check_perms('sc003')===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		if(isset($_POST['scname']))
		{
			if(!isset($scid))
			{
				$response->alert('请选择要修改的配置');
				$response->execute();
			}
			if(empty($scname))
			{
				$response->text_alert('error','scname','配置名称不能为空');
			}
			if(empty($scsql))
			{
				$response->text_alert('error','scsql','请设置源数据获取sql');
			}
			if(empty($other['up_time_column']))
			{
				$response->text_alert('error','other_up_time_column','请设置修改时间字段名');
			}
			if(!is_numeric($other['search_step']) || intval($other['search_step']) < 0)
			{
				$response->text_alert('error','other_search_step','请设置每次搜索步长，并确定设置的值为正整数');
			}
			if(empty($other['pk_name']))
			{
				$response->text_alert('error','other_pk_name','请设置主键字段名称');
			}
			if(empty($scconf))
			{
				$response->text_alert('error','scconf','请设置地址跳转配置');
			}
			$response->real_execute();
			$data = $_POST;
			$data['otherinfo'] = $other;
			unset($data['scid']);
			$res = $search->edit($scid,$data);
			if($res === false)
			{
				$response->alert('修改失败');
			}
			else 
			{
				$response->alert('修改成功');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			if(!isset($scid))
			{
				showmessage('请选择要修改的配置');
			}
			$info = $search->get_by_id($scid);
			if(!isset($info['scid']))
			{
				showmessage('你要修改的配置不存在');
			}
			include template('admin', 'search_conf_edit');
		}
		break;
	case 'del':
		if(check_perms('sc004')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
		}
		if(!isset($scid))
		{
			$response->alert('请选择要删除的配置');
		}
		$res = $search->del($scid);
		if($res === false)
		{
			$response->alert('删除失败');
		}
		else 
		{
			$response->alert('删除成功');
			$response->redirect($forward);
		}
		$response->execute();
		break;
	case 'search':
		$goto = $self_url;
		$goto .= '?action=datas';
		$goto .= (isset($source) && !empty($source)) ? '&source='."'$source'" : '';
		$goto .= (isset($url) && !empty($url)) ? '&url='.urlencode($url) : '';
		$goto .= (isset($fid) && !empty($fid)) ? '&fid='."'$fid'" : '';
		$response->redirect($goto);
		$response->execute();
		break;
	case 'datas':
		if(check_perms('sc007')===false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$source_list = $search->get_all();
		if(!is_array($source_list) || sizeof($source_list) <= 0)
		{
			showmessage('暂无数据源设置');
		}
		$search_data = new search();
		$scids = array_keys($source_list);
		if(isset($source) && $source <=0) 
		{
			showmessage('请选择数据源');
		}
		$source = isset($source) && in_array($source,$scids) ? $source : array_shift($scids);
		$url = isset($url) ? $url : '';
		$fid = isset($fid) ? $fid : 0;
		$page = isset($page) ? intval($page) : 1;
		$page = max(1,$page);
		$pagesize = 50;
		$list = $search_data->get_search_list($page,$pagesize,$source,$url,$fid);
		$pages = $search_data->pages;
		$forward = make_forward();
		include template('admin', 'search_data');
		break;
	case 'edit_data':
		$search_data = new search();
		if(isset($_POST['title']))
		{
			if(check_perms('sc008')===false)
			{
				showmessage('您没有权限进行此操作！');
			}
			$data = array(
				'title'		=>	$title,
				'content'	=>	$content,
				'url'		=>	$url,
				'addtime'	=>	strtotime($addtime)
			);
			$res = $search_data->edit_data($id,$data);
			if($res===false)
			{
				$response->alert('修改失败');
			}
			else 
			{
				$response->alert('修改成功');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			if(check_perms('sc008')===false)
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}	
			$id=isset($id) ? $id : 0;
			$info = $search_data->get_data_by_id($id);
			if(!isset($info['id']))
			{
				showmessage('您要修改的数据不存在');
			}
			$source = $search->get_by_id($info['source']);
			if(!isset($source['scid']))
			{
				showmessage('您要修改的数据不存在');
			}
			include template('admin', 'edit_data');
		}
		break;
	case 'del_data':
		if(check_perms('sc009')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
		}
		$id = isset($id) ? $id : 0;
		if(!empty($id))
		{
			$response->alert('请选择要删除的数据');
			$response->execute();
		}
		$search_data = new search();
		$res = $search_data->edit_data($id,array('status'=>0));
		if($res === false)
		{
			$response->alert('删除失败');
		}
		else 
		{
			$response->alert('删除成功');
			$response->redirect($forward);
		}
		$response->execute();
		break;
	case 'status':
		$status = intval($status==1) ? 1 : 0;
		if($status==1 && check_perms('sc005')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
		}
		if($status==0 && check_perms('sc006')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
		}
		if(!isset($scid))
		{
			$response->alert('请选择要修改的配置');
		}
		$info = $search->get_by_id($scid);
		if(!isset($info['scid']))
		{
			$response->alert('你要修改的配置不存在');
		}
		
		$res = $search->edit($scid,array('status'=>$status));
		if($res === false)
		{
			$response->alert('修改失败');
		}
		else 
		{
			$response->alert('修改成功');
			$response->redirect($forward);
		}
		$response->execute();
		break;
	case 'manage':
	default:
		if(check_perms('sc001')===false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$list = $search->get_all();
		$forward = make_forward();
		include template('admin', 'search_conf');
}
?>